// Shell based interface to the tag client
#include <stdio.h>
#include "tagdefs.h"
#include "getabsmod.h"
#include "client.h"
#include "nPatriciaTrie.h"
#include "linkedlist.h"

bool xml_output=false;

int main(int argc,char **argv){
	int i=0;
	long cnt;
	char       *file;
	linkedlist *lst;
	lst=new linkedlist();
	int n=1;


	/* Get the options first */
	if(argc>n && strcmp(argv[n], "-xml")==0)
	{
		xml_output=true;
		n++;
	}

	if (argc>n) {
		
#ifdef VERBOSE
		for (i=1;i<argc;i++)
			printf("%d : %s\n",i,argv[i]);
#endif

		if (strcmp(argv[n],"add")==0 && argc>n+2) {
			file = argv[n+1];
			for (i=n+2;i<argc;i++)
				lst->add(argv[i]);
			clientaddtags(file, lst);
		}
		else if (strcmp(argv[n],"remove")==0 && argc>n+2) {
			file  = argv[n+1];
			for (i=n+2;i<argc;i++)
				lst->add(argv[i]);
			clientremovetags(file, lst);
		}
		else if (strcmp(argv[n],"tagtriesize")==0) {
			cnt=clienttagsinindex();
			printf("%ld\n",cnt);
		}
		else if (strcmp(argv[n],"filetriesize")==0) {
			cnt=clientfilesinindex();
			printf("%ld\n",cnt);
		}
		else if (strcmp(argv[n],"listtags")==0 && argc>n+1) {
			lst=clientlisttags(argv[n+1]);
			while ((lst!=NULL) && (!lst->isEmpty()))
			{
				if(xml_output)
					printf("<tag>%s</tag>",lst->remove());
				else
					printf("%s\n",lst->remove());
			}
		}
		else if (strcmp(argv[n],"listfiles")==0 && argc>n+1) {
			getlistoffiles(argv[n+1]);
		}
// --------------------------------
// Introduced for debugging purposes		
//
		else if (strcmp(argv[n],"listallfiles")==0) {
			lst=clientlistallfiles();
			while ((lst!=NULL) && (!lst->isEmpty()))
				printf("%s\n",lst->remove());
		}
		else if (strcmp(argv[n],"getslashlist")==0) {
			getwholelist(false);
		}
		else if (strcmp(argv[n],"getwholelist")==0) {
			getwholelist(true);
		}
		else if (strcmp(argv[n],"listalltags")==0) {
			lst=clientlistalltags();
			while ((lst!=NULL) && (!lst->isEmpty()))
			{
				if(xml_output)
					printf("<tag>%s</tag>",lst->remove());
				else
					printf("%s\n",lst->remove());
			}

		}
		else if (strcmp(argv[n],"randomfile")==0) {
			lst=clientrandomfile();
			while ((lst!=NULL) && (!lst->isEmpty()))
				printf("%s\n",lst->remove());
		}
		else if (strcmp(argv[n],"randomtag")==0) {
			lst=clientrandomtag();
			while ((lst!=NULL) && (!lst->isEmpty()))
				printf("%s\n",lst->remove());
		}
		else if (((strcmp(argv[n],FLATSEARCH)==0) ||
			  (strcmp(argv[n],HIERARCHICAL)==0)) && argc>n+1) {
			linkedlist *tags;
			tags=new linkedlist();
			tags->add(argv[n+1]);
			lst=clientsearch(argv[n],tags);
			while ((lst!=NULL) && (!lst->isEmpty()))
			{
				if(xml_output)
					printf("<file>%s</file>",lst->remove());
				else
					printf("%s\n",lst->remove());
			}

			lst=clientgetsuggestions();
			while ((lst!=NULL) && (!lst->isEmpty()))
				printf("%s\n",lst->remove());
		}
// -----------------------------------
		else if (strcmp(argv[n],"copyfile")==0 && argc>n+2) {
			clientcopyfile(argv[n+1],argv[n+2]);
		}
		else if (strcmp(argv[n],"move")==0 && argc>n+2) {
			clientmovefile(argv[n+1],argv[n+2]);
		}
		else if (strcmp(argv[n],"removefile")==0 && argc>n+1) {
			clientremovefile(argv[n+1]);
		}
		else if (strcmp(argv[n],"removerecurse")==0 && argc>n+1) {
			clientremovefilerecurse(argv[n+1]);
		}
		else if (strcmp(argv[n],"endserver")==0) {
			clientendserver();
		}
		else if (strcmp(argv[n],"autotag")==0 && argc>n+1) {
			for (i=n+1;i<argc;i++)
				autotag(argv[i]);
		}
		else {
			printf("Unprocessed command \n");
			for (i=1;i<argc;i++)
				printf("%d : %s\n",i,argv[i]);
		}


	}
	else
	{
		printf("Unprocessed command \n");
		for (i=1;i<argc;i++)
			printf("%d : %s\n",i,argv[i]);
		printf("Reason: Invalid number of arguments\n");
	}

	lst->removeAll();
	delete lst;


	return 0;
}

void getwholelist(bool files_list)
{
	char *tmp;	
	linkedlist *lst = new linkedlist();

	lst=clientgetslashlist();
	while ((lst!=NULL) && (!lst->isEmpty()))
	{
		tmp = lst->remove();
		if(xml_output)
			printf("<dir>%s</dir>",tmp);
		else
			printf("%s\n",tmp);

		if(files_list)
			getlistoffiles(tmp);

		free(tmp);
	}

	lst->removeAll();
	delete lst;
}

void getlistoffiles(char *tag)
{
	char *tmp;	
	linkedlist *lst = new linkedlist();

	lst=clientlistfiles(tag);
	while ((lst!=NULL) && (!lst->isEmpty())) {
		tmp = lst->remove();
		if(xml_output)
			printf("<file>%s</file>",tmp);
		else
			printf("%s\n",tmp);
		free(tmp);
	}

	lst->removeAll();
	delete lst;
}
